<script lang="ts" setup>
import { ref,h } from 'vue';
import { useVbenDrawer } from '@vben/common-ui';
import { message } from 'ant-design-vue';
import { useVbenForm } from '#/adapter/form';
import { detailApi, editApi } from '#/api/system/setting';

const key = ref('');

const [Form, formApi] = useVbenForm({
  schema: [
    {
        fieldName: 'appid',
        label: 'AppID',
        component: 'Input',
    },
    {
        fieldName: 'appsecret',
        label: 'AppSecret',
        component: 'Input',
    },
    {
      component: 'Divider',
      fieldName: '',
      hideLabel: true,
      renderComponentContent: () => {
        return {
          default: () => h('div', '分享设置'),
        };
      },
    },
    {
        fieldName: 'share.title',
        label: '分享标题',
        component: 'Input',
    },
    {
        fieldName: 'share.path',
        label: '分享路径',
        component: 'Input',
    },
    {
        fieldName: 'share.imageUrl',
        component: 'ImageUpload',
        label: '朋友圈图片',
        description: '建议尺寸：300×300',
        componentProps: {
          maxCount: 1,
        },
    },
    {
        fieldName: 'share.imageUrl2',
        component: 'ImageUpload',
        label: '分享图片',
        description: '建议尺寸：500×400',
        componentProps: {
          maxCount: 1,
        },
    },
    {
      component: 'Divider',
      fieldName: '',
      hideLabel: true,
      renderComponentContent: () => {
        return {
          default: () => h('div', '其他设置'),
        };
      },
    },
    {
        fieldName: 'home.title',
        label: '首页标题',
        component: 'Input',
    },
    {
        fieldName: 'defaultAvatar',
        component: 'ImageUpload',
        label: '默认头像',
        description: '建议尺寸：300×300',
        componentProps: {
          maxCount: 1,
        },
    },
    {
      component: 'Divider',
      fieldName: '',
      hideLabel: true,
      renderComponentContent: () => {
        return {
          default: () => h('div', '首页海报'),
        };
      },
    },
    {
      fieldName: 'poster',
      label: '首页海报',
      component: 'Poster',
      hideLabel: true,
      // formItemClass: 'items-start',
      formItemClass: 'items-baseline',
      labelClass: 'pt-2',
      dependencies: {
        componentProps(){
            return {
                scene: 'home',
                page: 'pages/home/index',
                path: 'home.jpg'
            }
        },
        show: true,
        // 随意一个字段改变时，都会触发
        triggerFields: ['id'],
      },
    },
  ],
  showDefaultActions: false,
});

const [Drawer, drawerApi] = useVbenDrawer({
  async onConfirm() {
    const { valid } = await formApi.validate();
    if (!valid) return;
    const values = await formApi.getValues();
    drawerApi.lock();
    editApi(key.value, values)
      .then((res) => {
        message.success({
          content: res.message,
        });
        drawerApi.close();
      })
      .catch(() => {
        drawerApi.unlock();
      });
  },
  onOpenChange(isOpen) {
    if (isOpen) {
      const data = drawerApi.getData();
      formApi.resetForm();
      if (data.key) {
        key.value = data.key;
        drawerApi.setState({ title: data.describe })
        detailApi(data.key).then((res) => {
          formApi.setValues(res.values);
        })
      }
    }
  },
  overlayBlur: 1,
  title: '',
//   class: 'w-[380px]',
});
</script>
<template>
  <Drawer>
    <Form />
  </Drawer>
</template>
